// pages/my/myInfo/myInfo.js
const API = require('../../../API/api.js');
const app = getApp();
const WXBizDataCrypt = require('../../../utils/rdwx.js');
const util = require('../../../utils/util.js')
Page({
    getUserInfo: function () {
        API.getMyUserInfo({
            userId: wx.getStorageSync('userId')
        }).then((res) => {
            this.setData({
                level: res.data.level,
                isUser: res.data.mobile ? false : true,
                id: res.data.id,
                mobile: util.plusxing(res.data.mobile, 3, 4),
                userInfo: app.globalData.userInfo
            })
        })
    },
    getPhoneNumber(e) {
        console.log(e.detail.errMsg)
        console.log(e.detail.iv)
        console.log(e.detail.encryptedData)
        if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
            console.log('获取手机号失败')
        } else {
            var encryptedData = e.detail.encryptedData;
            var iv = e.detail.iv;
            var pc = new WXBizDataCrypt(app.globalData.appid, app.globalData.token.session_key);
            var data = pc.decryptData(encryptedData, iv);
            if (data.phoneNumber) {
                wx.setStorageSync('phone', data.phoneNumber)
                API.bindMobile({
                    userId: wx.getStorageSync('userId'),
                    mobile: data.phoneNumber
                }).then((res) => {
                    console.log(res);
                })
            }
            this.getUserInfo()
        }
    },
    /**
     * 页面的初始数据
     */
    data: {
        Height: app.globalData.Height,
        userInfo: {},
        level: '',
        mobile: null,
        id: '',
        imgUrl: app.globalData.imgUrl,
        isUser: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(app.globalData.userInfo)
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    gotoOrder() {
        wx.navigateTo({
            url: "../myInfo_order/myInfo_order"
        })
    },
    gotoShare() {
        wx.navigateTo({
            url: "../myInfo_share/myInfo_share"
        })
    },
    gotoAddress() {
        wx.navigateTo({
            url: '../myInfo_address/myInfo_address',
        })
    },
    gotoAdmin() {
        wx.navigateTo({
            url: '../myInfo_admin/myInfo_admin',
        })
    },
    gotoWallet() {

        wx.navigateTo({
            url: '../myInfo_wallet/myInfo_wallet',
        })
    }
})
